package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Project;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ProjectDao {
	
	private DBHelp<Project> db = new DBHelp<Project>();
	
	public void Save (String id,String name,String desc,String createtime) {
		String sql = "insert into t_project(id,name,desc,createtime) values(?,?,?,?)";
		db.executeSQL(sql, id,name,desc,createtime);
	}
	
	public void Del (String id) {
		String sql = "delete from t_project where id = ?";
		db.executeSQL(sql, id);
	}
	
	public void update (Project project) {
		String sql = "update t_project name = ?,desc = ?,createtime = ? where id = ?";
		db.executeSQL(sql, project.getName(),project.getDesc(),project.getCreatetime(),project.getId());
	}

	public List<Project> FindAll () {
		String sql = "select id,name,desc,createtime from t_project";
		return db.executeQueryForList(sql, new ProjectRowMapper());
	}
	
	public Project FindById (String id) {
		String sql = "select name,desc,createtime from t_project where id = ?";
		return db.executeQueryForObject(sql, new ProjectRowMapper(), id);
	}
	
	private class ProjectRowMapper implements RowMapper<Project> {
		
		public Project mapperRow (ResultSet rs) throws SQLException {
			
			Project project = new Project ();
			project.setId(rs.getString("id"));
			project.setName(rs.getString("name"));
			project.setDesc(rs.getString("desc"));
			project.setCreatetime(rs.getString("createtime"));
			return project;
			
		}
	}
}
